package com.gobo.eduservice.controller;


import com.gobo.commonutils.R;
import com.gobo.eduservice.pojo.EduChapter;
import com.gobo.eduservice.pojo.chapter.ChapterVo;
import com.gobo.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author gobo-generator
 * @since 2021-09-19
 */
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    @GetMapping("getAll/{courseId}")
    public R getChapterVoList(@PathVariable String courseId){
        List<ChapterVo> chapterVoList = chapterService.getChapterVoList(courseId);
        return R.ok().data("items",chapterVoList);
    }

    //通过Id查询章节信息
    @GetMapping("/getChapter/{chapterId}")
    public R getChapterById(@PathVariable String chapterId){
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("item",eduChapter);
    }

    //通过Id删除章节 (不能有小节)
    @PostMapping("/delete/{chapterId}")
    public R deleteChapterById(@PathVariable String chapterId){
        boolean b = chapterService.deleteChapter(chapterId);
        return b?R.ok():R.error();
    }

    //增加
    @PostMapping("/save")
    public R saveChapter(@RequestBody EduChapter eduChapter){
        boolean save = chapterService.save(eduChapter);
        return save?R.ok():R.error();
    }
    //修改
    @PostMapping("/update")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        boolean update = chapterService.updateById(eduChapter);
        return update?R.ok():R.error();
    }
}

